package com.supermarket.manage.provider.ucenter.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.supermarket.manage.common.constant.StatusConstant;
import com.supermarket.manage.common.dto.PageBean;
import com.supermarket.manage.common.dto.member.request.MemberPageParams;
import com.supermarket.manage.common.pojo.Member;
import com.supermarket.manage.common.utils.BigDecimalUtil;
import com.supermarket.manage.provider.api.ucenter.MemberManageService;
import com.supermarket.manage.provider.ucenter.mapper.MemberManageMapper;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author Faxon
 * @version 1.0
 * @date 2020-03-12 19:01
 **/
@Service(version = "1.0.0")
public class MemberManageServiceImpl implements MemberManageService {

    @Resource
    private MemberManageMapper memberManageMapper;

    @Override
    public Integer getCountByMphone(Member member) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mphone", member.getMphone())
                .eq("buyer_id", member.getBuyerId())
                .eq("status", StatusConstant.USE);
        return memberManageMapper.selectCount(wrapper);
    }

    @Override
    public Boolean addMember(Member member) {
        Date date = new Date();
        member.setStatus(StatusConstant.USE);
        member.setIntegral(BigDecimal.ZERO);
        member.setCreated(date);
        member.setUpdated(date);
        return memberManageMapper.insert(member) == 1;
    }

    @Override
    public Boolean addIntegral(String memberId, BigDecimal addValue) {
        return memberManageMapper.addIntegral(memberId,addValue) == 1;
    }

    @Override
    public PageBean<Member> getMemberPageList(MemberPageParams params) {
        PageHelper.startPage(params.getPage(), params.getSize());
        Page<Member> page = memberManageMapper.getMemberPageList(params);
        return new PageBean<>(page.getPageNum(), page.getPageSize(), page.getResult(), page.getTotal());
    }

    @Override
    public Boolean deleteMember(String mid) {
        Member newMember = new Member();
        newMember.setMid(mid);
        newMember.setStatus(StatusConstant.DELETED);
        newMember.setUpdated(new Date());
        return memberManageMapper.updateById(newMember) == 1;
    }

    @Override
    public Member getMemberById(String memberId) {
        return memberManageMapper.selectById(memberId);
    }

    @Override
    public List<Member> getMembersByIds(Set<String> ids) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.in("mid", ids);
        return memberManageMapper.selectList(wrapper);
    }

    @Override
    public List<Member> getMemberList(MemberPageParams params) {
        return memberManageMapper.getMemberList(params);
    }
}
